
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/22/2014 23:17:03
-- Generated from EDMX file: F:\_TFS20\tandaitanphuc\Source\TanDaiTanPhuc\DAL\Model\MasterModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [WHS];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ProductInventory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Inventories] DROP CONSTRAINT [FK_ProductInventory];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomerLiability]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Liabilities] DROP CONSTRAINT [FK_CustomerLiability];
GO
IF OBJECT_ID(N'[dbo].[FK_PackageTypeProduct]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Products] DROP CONSTRAINT [FK_PackageTypeProduct];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductCategoryProduct]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Products] DROP CONSTRAINT [FK_ProductCategoryProduct];
GO
IF OBJECT_ID(N'[dbo].[FK_UnitOfMeasureProduct]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Products] DROP CONSTRAINT [FK_UnitOfMeasureProduct];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductAttributeProduct]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Products] DROP CONSTRAINT [FK_ProductAttributeProduct];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomerStockIn]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockIns] DROP CONSTRAINT [FK_CustomerStockIn];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomerStockOut]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOuts] DROP CONSTRAINT [FK_CustomerStockOut];
GO
IF OBJECT_ID(N'[dbo].[FK_CustomerReceipt]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Receipts] DROP CONSTRAINT [FK_CustomerReceipt];
GO
IF OBJECT_ID(N'[dbo].[FK_WarehouseStockIn]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockIns] DROP CONSTRAINT [FK_WarehouseStockIn];
GO
IF OBJECT_ID(N'[dbo].[FK_WarehouseStockOut]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOuts] DROP CONSTRAINT [FK_WarehouseStockOut];
GO
IF OBJECT_ID(N'[dbo].[FK_WarehouseTransfer]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Transfers] DROP CONSTRAINT [FK_WarehouseTransfer];
GO
IF OBJECT_ID(N'[dbo].[FK_StockInStockOut]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOuts] DROP CONSTRAINT [FK_StockInStockOut];
GO
IF OBJECT_ID(N'[dbo].[FK_StockInTransfer]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Transfers] DROP CONSTRAINT [FK_StockInTransfer];
GO
IF OBJECT_ID(N'[dbo].[FK_TransferStockOut]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOuts] DROP CONSTRAINT [FK_TransferStockOut];
GO
IF OBJECT_ID(N'[dbo].[FK_StockInStockInDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockInDetails] DROP CONSTRAINT [FK_StockInStockInDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_TransferTransferDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TransferDetails] DROP CONSTRAINT [FK_TransferTransferDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_StockOutStockOutDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOutDetails] DROP CONSTRAINT [FK_StockOutStockOutDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductStockInDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockInDetails] DROP CONSTRAINT [FK_ProductStockInDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductTransferDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TransferDetails] DROP CONSTRAINT [FK_ProductTransferDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductStockOutDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOutDetails] DROP CONSTRAINT [FK_ProductStockOutDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_UnitOfMeasureStockInDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockInDetails] DROP CONSTRAINT [FK_UnitOfMeasureStockInDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_UnitOfMeasureTransferDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TransferDetails] DROP CONSTRAINT [FK_UnitOfMeasureTransferDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_UnitOfMeasureStockOutDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOutDetails] DROP CONSTRAINT [FK_UnitOfMeasureStockOutDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceiptStockOut]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockOuts] DROP CONSTRAINT [FK_ReceiptStockOut];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Warehouses]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Warehouses];
GO
IF OBJECT_ID(N'[dbo].[StockIns]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StockIns];
GO
IF OBJECT_ID(N'[dbo].[StockOuts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StockOuts];
GO
IF OBJECT_ID(N'[dbo].[Transfers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Transfers];
GO
IF OBJECT_ID(N'[dbo].[Receipts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Receipts];
GO
IF OBJECT_ID(N'[dbo].[PackageTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PackageTypes];
GO
IF OBJECT_ID(N'[dbo].[Inventories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Inventories];
GO
IF OBJECT_ID(N'[dbo].[Customers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Customers];
GO
IF OBJECT_ID(N'[dbo].[ProductCategories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProductCategories];
GO
IF OBJECT_ID(N'[dbo].[StockOutDetails]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StockOutDetails];
GO
IF OBJECT_ID(N'[dbo].[TransferDetails]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TransferDetails];
GO
IF OBJECT_ID(N'[dbo].[StockInDetails]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StockInDetails];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Products]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Products];
GO
IF OBJECT_ID(N'[dbo].[UnitOfMeasures]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UnitOfMeasures];
GO
IF OBJECT_ID(N'[dbo].[Liabilities]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Liabilities];
GO
IF OBJECT_ID(N'[dbo].[ProductAttributes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProductAttributes];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Warehouses'
CREATE TABLE [dbo].[Warehouses] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'StockIns'
CREATE TABLE [dbo].[StockIns] (
    [CustomerId] int  NOT NULL,
    [WarehouseId] int  NOT NULL,
    [StockedDate] datetime  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'StockOuts'
CREATE TABLE [dbo].[StockOuts] (
    [CustomerId] int  NOT NULL,
    [WarehouseId] int  NOT NULL,
    [StockInId] int  NOT NULL,
    [TransferId] int  NULL,
    [StockedDate] datetime  NULL,
    [Id] int  NOT NULL,
    [ReceiptId] int  NULL
);
GO

-- Creating table 'Transfers'
CREATE TABLE [dbo].[Transfers] (
    [WarehouseId] int  NOT NULL,
    [StockInId] int  NOT NULL,
    [StockedDate] datetime  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Receipts'
CREATE TABLE [dbo].[Receipts] (
    [CustomerId] int  NOT NULL,
    [Desciption] nvarchar(max)  NULL,
    [ReceiptAmount] decimal(18,0)  NULL,
    [Payer] nvarchar(max)  NULL,
    [Receiver] nvarchar(max)  NULL,
    [ReceiptedDate] datetime  NULL,
    [Id] int  NOT NULL,
    [StockOutId] int  NULL
);
GO

-- Creating table 'PackageTypes'
CREATE TABLE [dbo].[PackageTypes] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Inventories'
CREATE TABLE [dbo].[Inventories] (
    [ProductId] int  NOT NULL,
    [StockInQty] decimal(18,0)  NULL,
    [StockOutQty] decimal(18,0)  NULL,
    [IssuedDate] datetime  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Customers'
CREATE TABLE [dbo].[Customers] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [ContactName] nvarchar(max)  NULL,
    [Address] nvarchar(max)  NULL,
    [City] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'ProductCategories'
CREATE TABLE [dbo].[ProductCategories] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'StockOutDetails'
CREATE TABLE [dbo].[StockOutDetails] (
    [StockOutId] int  NOT NULL,
    [ProductId] int  NOT NULL,
    [Quantity] decimal(18,0)  NULL,
    [UnitOfMeasureId] int  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'TransferDetails'
CREATE TABLE [dbo].[TransferDetails] (
    [TransferId] int  NOT NULL,
    [ProductId] int  NOT NULL,
    [Quantity] decimal(18,0)  NULL,
    [UnitOfMeasureId] int  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'StockInDetails'
CREATE TABLE [dbo].[StockInDetails] (
    [StockInId] int  NOT NULL,
    [ProductId] int  NOT NULL,
    [Quantity] decimal(18,0)  NULL,
    [UnitOfMeasureId] int  NOT NULL,
    [QuantityInPaper] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [UserName] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Products'
CREATE TABLE [dbo].[Products] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Desciption] nvarchar(max)  NULL,
    [PackageTypeId] int  NULL,
    [ProductCategoryId] int  NULL,
    [UnitOfMeasureId] int  NULL,
    [ProductAttributeId] int  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'UnitOfMeasures'
CREATE TABLE [dbo].[UnitOfMeasures] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Liabilities'
CREATE TABLE [dbo].[Liabilities] (
    [CustomerId] int  NOT NULL,
    [Receipt] decimal(18,0)  NULL,
    [Spend] decimal(18,0)  NULL,
    [IssuedDate] datetime  NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'ProductAttributes'
CREATE TABLE [dbo].[ProductAttributes] (
    [Code] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NULL,
    [Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Warehouses'
ALTER TABLE [dbo].[Warehouses]
ADD CONSTRAINT [PK_Warehouses]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StockIns'
ALTER TABLE [dbo].[StockIns]
ADD CONSTRAINT [PK_StockIns]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StockOuts'
ALTER TABLE [dbo].[StockOuts]
ADD CONSTRAINT [PK_StockOuts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Transfers'
ALTER TABLE [dbo].[Transfers]
ADD CONSTRAINT [PK_Transfers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Receipts'
ALTER TABLE [dbo].[Receipts]
ADD CONSTRAINT [PK_Receipts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PackageTypes'
ALTER TABLE [dbo].[PackageTypes]
ADD CONSTRAINT [PK_PackageTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Inventories'
ALTER TABLE [dbo].[Inventories]
ADD CONSTRAINT [PK_Inventories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Customers'
ALTER TABLE [dbo].[Customers]
ADD CONSTRAINT [PK_Customers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ProductCategories'
ALTER TABLE [dbo].[ProductCategories]
ADD CONSTRAINT [PK_ProductCategories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StockOutDetails'
ALTER TABLE [dbo].[StockOutDetails]
ADD CONSTRAINT [PK_StockOutDetails]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'TransferDetails'
ALTER TABLE [dbo].[TransferDetails]
ADD CONSTRAINT [PK_TransferDetails]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StockInDetails'
ALTER TABLE [dbo].[StockInDetails]
ADD CONSTRAINT [PK_StockInDetails]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Products'
ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [PK_Products]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UnitOfMeasures'
ALTER TABLE [dbo].[UnitOfMeasures]
ADD CONSTRAINT [PK_UnitOfMeasures]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Liabilities'
ALTER TABLE [dbo].[Liabilities]
ADD CONSTRAINT [PK_Liabilities]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ProductAttributes'
ALTER TABLE [dbo].[ProductAttributes]
ADD CONSTRAINT [PK_ProductAttributes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ProductId] in table 'Inventories'
ALTER TABLE [dbo].[Inventories]
ADD CONSTRAINT [FK_ProductInventory]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductInventory'
CREATE INDEX [IX_FK_ProductInventory]
ON [dbo].[Inventories]
    ([ProductId]);
GO

-- Creating foreign key on [CustomerId] in table 'Liabilities'
ALTER TABLE [dbo].[Liabilities]
ADD CONSTRAINT [FK_CustomerLiability]
    FOREIGN KEY ([CustomerId])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomerLiability'
CREATE INDEX [IX_FK_CustomerLiability]
ON [dbo].[Liabilities]
    ([CustomerId]);
GO

-- Creating foreign key on [PackageTypeId] in table 'Products'
ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [FK_PackageTypeProduct]
    FOREIGN KEY ([PackageTypeId])
    REFERENCES [dbo].[PackageTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PackageTypeProduct'
CREATE INDEX [IX_FK_PackageTypeProduct]
ON [dbo].[Products]
    ([PackageTypeId]);
GO

-- Creating foreign key on [ProductCategoryId] in table 'Products'
ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [FK_ProductCategoryProduct]
    FOREIGN KEY ([ProductCategoryId])
    REFERENCES [dbo].[ProductCategories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductCategoryProduct'
CREATE INDEX [IX_FK_ProductCategoryProduct]
ON [dbo].[Products]
    ([ProductCategoryId]);
GO

-- Creating foreign key on [UnitOfMeasureId] in table 'Products'
ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [FK_UnitOfMeasureProduct]
    FOREIGN KEY ([UnitOfMeasureId])
    REFERENCES [dbo].[UnitOfMeasures]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UnitOfMeasureProduct'
CREATE INDEX [IX_FK_UnitOfMeasureProduct]
ON [dbo].[Products]
    ([UnitOfMeasureId]);
GO

-- Creating foreign key on [ProductAttributeId] in table 'Products'
ALTER TABLE [dbo].[Products]
ADD CONSTRAINT [FK_ProductAttributeProduct]
    FOREIGN KEY ([ProductAttributeId])
    REFERENCES [dbo].[ProductAttributes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductAttributeProduct'
CREATE INDEX [IX_FK_ProductAttributeProduct]
ON [dbo].[Products]
    ([ProductAttributeId]);
GO

-- Creating foreign key on [CustomerId] in table 'StockIns'
ALTER TABLE [dbo].[StockIns]
ADD CONSTRAINT [FK_CustomerStockIn]
    FOREIGN KEY ([CustomerId])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomerStockIn'
CREATE INDEX [IX_FK_CustomerStockIn]
ON [dbo].[StockIns]
    ([CustomerId]);
GO

-- Creating foreign key on [CustomerId] in table 'StockOuts'
ALTER TABLE [dbo].[StockOuts]
ADD CONSTRAINT [FK_CustomerStockOut]
    FOREIGN KEY ([CustomerId])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomerStockOut'
CREATE INDEX [IX_FK_CustomerStockOut]
ON [dbo].[StockOuts]
    ([CustomerId]);
GO

-- Creating foreign key on [CustomerId] in table 'Receipts'
ALTER TABLE [dbo].[Receipts]
ADD CONSTRAINT [FK_CustomerReceipt]
    FOREIGN KEY ([CustomerId])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CustomerReceipt'
CREATE INDEX [IX_FK_CustomerReceipt]
ON [dbo].[Receipts]
    ([CustomerId]);
GO

-- Creating foreign key on [WarehouseId] in table 'StockIns'
ALTER TABLE [dbo].[StockIns]
ADD CONSTRAINT [FK_WarehouseStockIn]
    FOREIGN KEY ([WarehouseId])
    REFERENCES [dbo].[Warehouses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WarehouseStockIn'
CREATE INDEX [IX_FK_WarehouseStockIn]
ON [dbo].[StockIns]
    ([WarehouseId]);
GO

-- Creating foreign key on [WarehouseId] in table 'StockOuts'
ALTER TABLE [dbo].[StockOuts]
ADD CONSTRAINT [FK_WarehouseStockOut]
    FOREIGN KEY ([WarehouseId])
    REFERENCES [dbo].[Warehouses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WarehouseStockOut'
CREATE INDEX [IX_FK_WarehouseStockOut]
ON [dbo].[StockOuts]
    ([WarehouseId]);
GO

-- Creating foreign key on [WarehouseId] in table 'Transfers'
ALTER TABLE [dbo].[Transfers]
ADD CONSTRAINT [FK_WarehouseTransfer]
    FOREIGN KEY ([WarehouseId])
    REFERENCES [dbo].[Warehouses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WarehouseTransfer'
CREATE INDEX [IX_FK_WarehouseTransfer]
ON [dbo].[Transfers]
    ([WarehouseId]);
GO

-- Creating foreign key on [StockInId] in table 'StockOuts'
ALTER TABLE [dbo].[StockOuts]
ADD CONSTRAINT [FK_StockInStockOut]
    FOREIGN KEY ([StockInId])
    REFERENCES [dbo].[StockIns]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StockInStockOut'
CREATE INDEX [IX_FK_StockInStockOut]
ON [dbo].[StockOuts]
    ([StockInId]);
GO

-- Creating foreign key on [StockInId] in table 'Transfers'
ALTER TABLE [dbo].[Transfers]
ADD CONSTRAINT [FK_StockInTransfer]
    FOREIGN KEY ([StockInId])
    REFERENCES [dbo].[StockIns]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StockInTransfer'
CREATE INDEX [IX_FK_StockInTransfer]
ON [dbo].[Transfers]
    ([StockInId]);
GO

-- Creating foreign key on [TransferId] in table 'StockOuts'
ALTER TABLE [dbo].[StockOuts]
ADD CONSTRAINT [FK_TransferStockOut]
    FOREIGN KEY ([TransferId])
    REFERENCES [dbo].[Transfers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TransferStockOut'
CREATE INDEX [IX_FK_TransferStockOut]
ON [dbo].[StockOuts]
    ([TransferId]);
GO

-- Creating foreign key on [StockInId] in table 'StockInDetails'
ALTER TABLE [dbo].[StockInDetails]
ADD CONSTRAINT [FK_StockInStockInDetail]
    FOREIGN KEY ([StockInId])
    REFERENCES [dbo].[StockIns]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StockInStockInDetail'
CREATE INDEX [IX_FK_StockInStockInDetail]
ON [dbo].[StockInDetails]
    ([StockInId]);
GO

-- Creating foreign key on [TransferId] in table 'TransferDetails'
ALTER TABLE [dbo].[TransferDetails]
ADD CONSTRAINT [FK_TransferTransferDetail]
    FOREIGN KEY ([TransferId])
    REFERENCES [dbo].[Transfers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TransferTransferDetail'
CREATE INDEX [IX_FK_TransferTransferDetail]
ON [dbo].[TransferDetails]
    ([TransferId]);
GO

-- Creating foreign key on [StockOutId] in table 'StockOutDetails'
ALTER TABLE [dbo].[StockOutDetails]
ADD CONSTRAINT [FK_StockOutStockOutDetail]
    FOREIGN KEY ([StockOutId])
    REFERENCES [dbo].[StockOuts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StockOutStockOutDetail'
CREATE INDEX [IX_FK_StockOutStockOutDetail]
ON [dbo].[StockOutDetails]
    ([StockOutId]);
GO

-- Creating foreign key on [ProductId] in table 'StockInDetails'
ALTER TABLE [dbo].[StockInDetails]
ADD CONSTRAINT [FK_ProductStockInDetail]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductStockInDetail'
CREATE INDEX [IX_FK_ProductStockInDetail]
ON [dbo].[StockInDetails]
    ([ProductId]);
GO

-- Creating foreign key on [ProductId] in table 'TransferDetails'
ALTER TABLE [dbo].[TransferDetails]
ADD CONSTRAINT [FK_ProductTransferDetail]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductTransferDetail'
CREATE INDEX [IX_FK_ProductTransferDetail]
ON [dbo].[TransferDetails]
    ([ProductId]);
GO

-- Creating foreign key on [ProductId] in table 'StockOutDetails'
ALTER TABLE [dbo].[StockOutDetails]
ADD CONSTRAINT [FK_ProductStockOutDetail]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductStockOutDetail'
CREATE INDEX [IX_FK_ProductStockOutDetail]
ON [dbo].[StockOutDetails]
    ([ProductId]);
GO

-- Creating foreign key on [UnitOfMeasureId] in table 'StockInDetails'
ALTER TABLE [dbo].[StockInDetails]
ADD CONSTRAINT [FK_UnitOfMeasureStockInDetail]
    FOREIGN KEY ([UnitOfMeasureId])
    REFERENCES [dbo].[UnitOfMeasures]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UnitOfMeasureStockInDetail'
CREATE INDEX [IX_FK_UnitOfMeasureStockInDetail]
ON [dbo].[StockInDetails]
    ([UnitOfMeasureId]);
GO

-- Creating foreign key on [UnitOfMeasureId] in table 'TransferDetails'
ALTER TABLE [dbo].[TransferDetails]
ADD CONSTRAINT [FK_UnitOfMeasureTransferDetail]
    FOREIGN KEY ([UnitOfMeasureId])
    REFERENCES [dbo].[UnitOfMeasures]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UnitOfMeasureTransferDetail'
CREATE INDEX [IX_FK_UnitOfMeasureTransferDetail]
ON [dbo].[TransferDetails]
    ([UnitOfMeasureId]);
GO

-- Creating foreign key on [UnitOfMeasureId] in table 'StockOutDetails'
ALTER TABLE [dbo].[StockOutDetails]
ADD CONSTRAINT [FK_UnitOfMeasureStockOutDetail]
    FOREIGN KEY ([UnitOfMeasureId])
    REFERENCES [dbo].[UnitOfMeasures]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UnitOfMeasureStockOutDetail'
CREATE INDEX [IX_FK_UnitOfMeasureStockOutDetail]
ON [dbo].[StockOutDetails]
    ([UnitOfMeasureId]);
GO

-- Creating foreign key on [ReceiptId] in table 'StockOuts'
ALTER TABLE [dbo].[StockOuts]
ADD CONSTRAINT [FK_ReceiptStockOut]
    FOREIGN KEY ([ReceiptId])
    REFERENCES [dbo].[Receipts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceiptStockOut'
CREATE INDEX [IX_FK_ReceiptStockOut]
ON [dbo].[StockOuts]
    ([ReceiptId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------